/**
 * 
 */
package info.niwota.start.contact;

import info.niwota.start.Debug;
import info.niwota.start.Preferences;
import info.niwota.start.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

/**
 * @author qiangli
 *
 */
public class EditContactActivity extends Activity {

	private static final String TAG = "EditContactActivity";
	
	private EditText vName;
	private EditText vPhone;
	private EditText vNote;
	private String key;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    setContentView(R.layout.contact_edit);
	    
	    vName = (EditText) findViewById(R.id.contact_name);
	    vPhone = (EditText) findViewById(R.id.contact_phone);
	    vNote = (EditText) findViewById(R.id.contact_note);
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (Debug.DEBUG) {
			Log.d(TAG, "onPause key: " + key);
		}
		Contact o = new Contact();
		try {
			o.name = vName.getText().toString();
			o.phone = vPhone.getText().toString();
			o.note = vNote.getText().toString();
			
			Preferences.putContact(this, key, o);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		key = getIntent().getData().getFragment();
		
		if (Debug.DEBUG) {
			Log.d(TAG, "onResume key: " + key);
		}
		
		try {
			setTitle(getResources().getString(R.string.fmt_edit_contact, key));
			
			Contact o = Preferences.getContact(this, key);
			vName.setText(o.name);
			vPhone.setText(o.phone);
			vNote.setText(o.note);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
